Bitcrush: Use industry-standard quantization formula for signed audio#465
Open
gijzelaerr wants to merge 2 commits intospotify:masterfrom
Open
Bitcrush: Use industry-standard quantization formula for signed audio#465gijzelaerr wants to merge 2 commits intospotify:masterfrom
gijzelaerr wants to merge 2 commits intospotify:masterfrom
Conversation
…metry The scale factor was `pow(2, bitDepth)` which doesn't properly center quantization around zero for signed audio. Changed to `pow(2, bitDepth - 1) + 1` so that the quantization is symmetric. Added tests verifying quantization correctness, symmetry around zero, output range for signed audio, edge cases (bit_depth=1 and 16), and expected number of quantization levels. Fixes spotify#396 Related: spotify#397 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The previous fix used pow(2, bitDepth - 1) + 1 which, while symmetric, can cause the negative peak (-2^(n-1)) to exceed -1.0 and clip. The industry standard (used by FFmpeg, JUCE, VST SDKs) is pow(2, bitDepth - 1) which maps the range to [-1.0, 0.9999...], preserving full dynamic range without risk of clipping. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Member
Author
|
Note: The CI failures ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pow(2, bitDepth)(unsigned) topow(2, bitDepth - 1)(industry standard for signed audio)[-1.0, 0.9999...], matching FFmpeg, JUCE, and VST SDK conventionsCloses #396
Supersedes #397
Test plan
test_output_is_quantized- output samples snap to discrete quantized levels (bit_depth 1, 2, 4, 8, 16)test_bit_depth_8_output_range_signed- output stays in [-1, 1] with both positive and negative valuestest_quantization_symmetric_around_zero-quantize(-x) == -quantize(x)(bit_depth 2, 4, 8, 16)test_bit_depth_1_produces_few_levels- edge case for minimum useful bit depthtest_bit_depth_16_preserves_signal_closely- high bit depth produces near-lossless outputtest_number_of_quantization_levels- distinct output levels match expected count🤖 Generated with Claude Code